<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Recovery extends Model
{
    // 表名
    protected $name = 'recovery';
    // 开启自动写入时间戳字段
    protected $hidden = [];
    // 追加属性
    protected $append = [
    ];

    public function base($query)
    {
        $query->where(['status' => 1]);
    }

    /**
     * 获取指导价
     * @param array $params
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAGuidePrice($params = [])
    {
        $region_id      = !empty($params['region_id']) ? $params['region_id'] : 0;
        $categoryList   = (new Category())->field('id,type,flag,name,image,keywords,description')->where(['type' => 'recovery', 'status' => 'normal'])->select();
        $list           = $this->select();
        $customList     = [];

        if ($region_id) {
            $customList = Db::name('RecoveryCustom')->where(['region_id' => $region_id, 'status' => 1])->order('id DESC')->column('*', 'recovery_id');
        }

        foreach ($categoryList as &$v) {
            $v          = $v->getData();
            $v['image'] = cdnurl($v['image'], true);
            foreach ($list as &$v2) {
                if ($v['id'] == $v2['category_id']) {
                    if ($region_id && isset($customList[$v2['id']]['price'])) {
                        $v2['custom_price'] = $customList[$v2['id']]['price'];
                    }
                    $v['sons'][] = $v2;
                }
            }
        }

        return $categoryList;
    }
}